package net.bbtang.website.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.bbtang.website.bean.UserGroupBean;
import net.bbtang.website.mapper.OrderMapper;
import net.bbtang.website.mapper.UserMapper;
import net.bbtang.website.mapper.UserPublishInfoMapper;
import net.bbtang.website.pojo.OrderInfoDTO;
import net.bbtang.website.pojo.UserPublishInfoDTO;
import net.bbtang.website.service.GroupInfoService;
import net.bbtang.website.service.OrderInfoService;
import net.bbtang.website.utils.OrderConstantsStatus;




@Service("orderInfoService")
public class OrderInfoServiceImpl implements OrderInfoService {
	
	@Autowired
	OrderMapper orderMapper;
	@Autowired
	UserMapper userMapper;
	@Autowired
	GroupInfoService groupInfoService;
	@Autowired
	UserPublishInfoMapper userPublishInfoMapper;

	@Override
	public int initOrder(OrderInfoDTO orderInfo) {
		return orderMapper.initOrder(orderInfo);
	}

	@Override
	public void updateOrderType(OrderInfoDTO orderInfo) {
		orderMapper.updateOrderType(orderInfo);
	}

	@Override
	public List<OrderInfoDTO> getOrderInfoList(OrderInfoDTO orderInfo) {
		List<OrderInfoDTO> orderInfoList = orderMapper.getOrderInfoList(orderInfo);
		if(orderInfoList != null && orderInfoList.size() > 0){
			for (OrderInfoDTO orderInfoDTO : orderInfoList) {
				//根据accept_user_id获取接受者信息
				Map<String,Object> mapAccess = new HashMap<String, Object>();
				if(orderInfoDTO.getAcceptUserId() != null){
					mapAccess.put("userId", orderInfoDTO.getAcceptUserId());
					orderInfoDTO.setAccessUserInfo(userMapper.getUserInfoById(mapAccess).get(0));
				}
				
				//根据user_id获取用户信息
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("userId", orderInfoDTO.getUserId());
				orderInfoDTO.setUserInfo(userMapper.getUserInfoById(map).get(0));
				
				//根据publich_id获取小组meal信息
				if(orderInfoDTO.getPublishType().equals(OrderConstantsStatus.ORDER_GROUP)){
					Map<String,Object> mapGroup = new HashMap<String, Object>();
					mapGroup.put("groupId", orderInfoDTO.getPublishId());
					mapGroup.put("mealId", orderInfoDTO.getMealId());
					List<UserGroupBean> groupList = groupInfoService.getGroupInfoByid(mapGroup);
					if(groupList!= null && groupList.size() > 0){
						orderInfoDTO.setGroupInfo(groupList.get(0));
					}
				}else{
					//根据发布id获取发布信息
					List<UserPublishInfoDTO> publishInfoList = userPublishInfoMapper.search(0, orderInfoDTO.getUserId(), orderInfoDTO.getPublishId(), null, null,null);
					if(publishInfoList != null && publishInfoList.size() > 0){
						orderInfoDTO.setPublishInfo(publishInfoList.get(0));
					}
					
				}
				
			}
			
		}

		
		
		return orderInfoList;
	}
	


	


}
